<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>Luat开源项目脚本lib</title>
    <link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>script_LuaTask lib</h1>

<ul>
  <li><a href="../index.html">Index</a></li>
</ul>

<h2>Contents</h2>
<ul>
<li><a href="#Functions">Functions</a></li>
</ul>


<h2>Modules</h2>
<ul class="nowrap">
  <li><a href="../modules/aLiYun.html">aLiYun</a></li>
  <li><a href="../modules/aLiYunOta.html">aLiYunOta</a></li>
  <li><a href="../modules/agps.html">agps</a></li>
  <li><a href="../modules/agpsHxxt.html">agpsHxxt</a></li>
  <li><a href="../modules/agpsZkw.html">agpsZkw</a></li>
  <li><a href="../modules/audio.html">audio</a></li>
  <li><a href="../modules/cc.html">cc</a></li>
  <li><a href="../modules/clib.html">clib</a></li>
  <li><a href="../modules/common.html">common</a></li>
  <li><a href="../modules/console.html">console</a></li>
  <li><a href="../modules/errDump.html">errDump</a></li>
  <li><a href="../modules/ftp.html">ftp</a></li>
  <li><a href="../modules/gps.html">gps</a></li>
  <li><a href="../modules/gpsHxxt.html">gpsHxxt</a></li>
  <li><a href="../modules/gpsZkw.html">gpsZkw</a></li>
  <li><a href="../modules/http.html">http</a></li>
  <li><a href="../modules/lbsLoc.html">lbsLoc</a></li>
  <li><a href="../modules/led.html">led</a></li>
  <li><a href="../modules/link.html">link</a></li>
  <li><a href="../modules/log.html">log</a></li>
  <li><a href="../modules/misc.html">misc</a></li>
  <li><strong>mqtt</strong></li>
  <li><a href="../modules/net.html">net</a></li>
  <li><a href="../modules/netLed.html">netLed</a></li>
  <li><a href="../modules/ntp.html">ntp</a></li>
  <li><a href="../modules/nvm.html">nvm</a></li>
  <li><a href="../modules/patch.html">patch</a></li>
  <li><a href="../modules/pb.html">pb</a></li>
  <li><a href="../modules/pins.html">pins</a></li>
  <li><a href="../modules/pm.html">pm</a></li>
  <li><a href="../modules/powerKey.html">powerKey</a></li>
  <li><a href="../modules/record.html">record</a></li>
  <li><a href="../modules/ril.html">ril</a></li>
  <li><a href="../modules/scanCode.html">scanCode</a></li>
  <li><a href="../modules/sim.html">sim</a></li>
  <li><a href="../modules/sms.html">sms</a></li>
  <li><a href="../modules/socket.html">socket</a></li>
  <li><a href="../modules/sys.html">sys</a></li>
  <li><a href="../modules/uiWin.html">uiWin</a></li>
  <li><a href="../modules/update.html">update</a></li>
  <li><a href="../modules/utils.html">utils</a></li>
  <li><a href="../modules/websocket.html">websocket</a></li>
  <li><a href="../modules/wifiScan.html">wifiScan</a></li>
</ul>

</div>

<div id="content">

<h1>Module <code>mqtt</code></h1>
<p>模块功能：MQTT客户端</p>
<p></p>
    <h3>Info:</h3>
    <ul>
        <li><strong>Copyright</strong>: openLuat</li>
        <li><strong>Release</strong>: 2017.10.24</li>
        <li><strong>License</strong>: MIT</li>
        <li><strong>Author</strong>: openLuat</li>
    </ul>


<h2><a href="#Functions">Functions</a></h2>
<table class="function_list">
	<tr>
	<td class="name" nowrap><a href="#client">
    client (clientId<strong  style="color: gray;">[, keepAlive=300]</strong><strong  style="color: gray;">[, username=""]</strong><strong  style="color: gray;">[, password=""]</strong><strong  style="color: gray;">[, cleanSession=1]</strong><strong  style="color: gray;">[, will=nil]</strong><strong  style="color: gray;">[, version="3.1.1"]</strong>)
    </a></td>
	<td class="summary">创建一个mqtt client实例</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:connect">
    mqttc:connect (host, port<strong  style="color: gray;">[, transport="tcp"]</strong><strong  style="color: gray;">[, cert=nil]</strong><strong  style="color: gray;">[, timeout=120]</strong>)
    </a></td>
	<td class="summary">连接mqtt服务器</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:subscribe">
    mqttc:subscribe (topic<strong  style="color: gray;">[, qos=0]</strong>)
    </a></td>
	<td class="summary">订阅主题</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:unsubscribe">
    mqttc:unsubscribe (topic)
    </a></td>
	<td class="summary">取消订阅主题</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:publish">
    mqttc:publish (topic, payload<strong  style="color: gray;">[, qos=0]</strong><strong  style="color: gray;">[, retain=0]</strong>)
    </a></td>
	<td class="summary">发布一条消息</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:receive">
    mqttc:receive (timeout<strong  style="color: gray;">[, msg=nil]</strong>)
    </a></td>
	<td class="summary">接收消息</td>
	</tr>
	<tr>
	<td class="name" nowrap><a href="#mqttc:disconnect">
    mqttc:disconnect ()
    </a></td>
	<td class="summary">断开与服务器的连接</td>
	</tr>
</table>

<br/>
<br/>


    <h2 class="section-header "><a name="Functions"></a>Functions</h2>

    <dl class="function">
    <dt>
    <a name = "client"></a>
    <strong  style="color: red;">
    client (clientId<strong  style="color: green;">[, keepAlive=300]</strong><strong  style="color: green;">[, username=""]</strong><strong  style="color: green;">[, password=""]</strong><strong  style="color: green;">[, cleanSession=1]</strong><strong  style="color: green;">[, will=nil]</strong><strong  style="color: green;">[, version="3.1.1"]</strong>)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">创建一个mqtt client实例</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">clientId</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
        <pre class="example"></pre>
        </li>
        <li><span class="parameter">keepAlive</span>
            <span class="types"><span class="type">number</span></span>
         [<em>此参数可选，默认值为：</em> 300]
        <pre class="example"> 心跳间隔(单位为秒)，默认300秒</pre>
        </li>
        <li><span class="parameter">username</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
         [<em>此参数可选，默认值为：</em> ""]
        <pre class="example"> 用户名，用户名为空配置为""或者nil</pre>
        </li>
        <li><span class="parameter">password</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
         [<em>此参数可选，默认值为：</em> ""]
        <pre class="example"> 密码，密码为空配置为""或者nil</pre>
        </li>
        <li><span class="parameter">cleanSession</span>
            <span class="types"><span class="type">number</span></span>
         [<em>此参数可选，默认值为：</em> 1]
        <pre class="example"> 1/0</pre>
        </li>
        <li><span class="parameter">will</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.5">table</a></span>
         [<em>此参数可选，默认值为：</em> nil]
        <pre class="example"> 遗嘱参数，格式为{qos=, retain=, topic=, payload=}</pre>
        </li>
        <li><span class="parameter">version</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
         [<em>此参数可选，默认值为：</em> "3.1.1"]
        <pre class="example"> MQTT版本号，仅支持"3.1"和"3.1.1"</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">table mqttc client实例</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc = mqtt.client(<span class="string">"clientid-123"</span>)
mqttc = mqtt.client(<span class="string">"clientid-123"</span>,<span class="number">200</span>)
mqttc = mqtt.client(<span class="string">"clientid-123"</span>,<span class="keyword">nil</span>,<span class="string">"user"</span>,<span class="string">"password"</span>)
mqttc = mqtt.client(<span class="string">"clientid-123"</span>,<span class="keyword">nil</span>,<span class="string">"user"</span>,<span class="string">"password"</span>,<span class="keyword">nil</span>,{qos=<span class="number">0</span>,retain=<span class="number">0</span>,topic=<span class="string">"willTopic"</span>,payload=<span class="string">"willTopic"</span>},<span class="string">"3.1"</span>)</pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:connect"></a>
    <strong  style="color: red;">
    mqttc:connect (host, port<strong  style="color: green;">[, transport="tcp"]</strong><strong  style="color: green;">[, cert=nil]</strong><strong  style="color: green;">[, timeout=120]</strong>)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">连接mqtt服务器</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">host</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
        <pre class="example"> 服务器地址</pre>
        </li>
        <li><span class="parameter">port</span>
        <pre class="example"> string或者number类型，服务器端口</pre>
        </li>
        <li><span class="parameter">transport</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
         [<em>此参数可选，默认值为：</em> "tcp"]
        <pre class="example"> "tcp"或者"tcp_ssl"</pre>
        </li>
        <li><span class="parameter">cert</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.5">table</a></span>
         [<em>此参数可选，默认值为：</em> nil]
        <pre class="example">table或者nil类型，ssl证书，当transport为"tcp_ssl"时，此参数才有意义。cert格式如下：
 {
     caCert = "ca.crt", --CA证书文件(Base64编码 X.509格式)，如果存在此参数，则表示客户端会对服务器的证书进行校验；不存在则不校验
     clientCert = "client.crt", --客户端证书文件(Base64编码 X.509格式)，服务器对客户端的证书进行校验时会用到此参数
     clientKey = "client.key", --客户端私钥文件(Base64编码 X.509格式)
     clientPassword = "123456", --客户端证书文件密码[可选]
 }</pre>
        </li>
        <li><span class="parameter">timeout</span>
            <span class="types"><span class="type">number</span></span>
         [<em>此参数可选，默认值为：</em> 120]
        <pre class="example"> 可选参数，socket连接超时时间，单位秒</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">result true表示成功，false或者nil表示失败</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc = mqtt.client(<span class="string">"clientid-123"</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">false</span>); mqttc:connect(<span class="string">"mqttserver.com"</span>, <span class="number">1883</span>, <span class="string">"tcp"</span>, <span class="number">5</span>)</pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:subscribe"></a>
    <strong  style="color: red;">
    mqttc:subscribe (topic<strong  style="color: green;">[, qos=0]</strong>)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">订阅主题</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">topic</span>
        <pre class="example">string或者table类型，一个主题时为string类型，多个主题时为table类型，主题内容为UTF8编码</pre>
        </li>
        <li><span class="parameter">qos</span>
         [<em>此参数可选，默认值为：</em> 0]
        <pre class="example">number或者nil，topic为一个主题时，qos为number类型(0/1/2，默认0)；topic为多个主题时，qos为nil</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">bool true表示成功，false或者nil表示失败</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc:subscribe(<span class="string">"/abc"</span>, <span class="number">0</span>) <span class="comment">-- subscribe topic "/abc" with qos = 0
</span>mqttc:subscribe({[<span class="string">"/topic1"</span>] = <span class="number">0</span>, [<span class="string">"/topic2"</span>] = <span class="number">1</span>, [<span class="string">"/topic3"</span>] = <span class="number">2</span>}) <span class="comment">-- subscribe multi topic</span></pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:unsubscribe"></a>
    <strong  style="color: red;">
    mqttc:unsubscribe (topic)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">取消订阅主题</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">topic</span>
        <pre class="example">string或者table类型，一个主题时为string类型，多个主题时为table类型，主题内容为UTF8编码</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">bool true表示成功，false或者nil表示失败</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc:unsubscribe(<span class="string">"/abc"</span>) <span class="comment">-- unsubscribe topic "/abc"
</span>mqttc:unsubscribe({<span class="string">"/topic1"</span>, <span class="string">"/topic2"</span>, <span class="string">"/topic3"</span>}) <span class="comment">-- unsubscribe multi topic</span></pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:publish"></a>
    <strong  style="color: red;">
    mqttc:publish (topic, payload<strong  style="color: green;">[, qos=0]</strong><strong  style="color: green;">[, retain=0]</strong>)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">发布一条消息</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">topic</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
        <pre class="example"> UTF8编码的字符串</pre>
        </li>
        <li><span class="parameter">payload</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
        <pre class="example"> 用户自己控制payload的编码，mqtt.lua不会对payload做任何编码转换</pre>
        </li>
        <li><span class="parameter">qos</span>
            <span class="types"><span class="type">number</span></span>
         [<em>此参数可选，默认值为：</em> 0]
        <pre class="example"> 0/1/2, default 0</pre>
        </li>
        <li><span class="parameter">retain</span>
            <span class="types"><span class="type">number</span></span>
         [<em>此参数可选，默认值为：</em> 0]
        <pre class="example"> 0或者1</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">bool 发布成功返回true，失败返回false</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc = mqtt.client(<span class="string">"clientid-123"</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">false</span>)
mqttc:connect(<span class="string">"mqttserver.com"</span>, <span class="number">1883</span>, <span class="string">"tcp"</span>)
mqttc:publish(<span class="string">"/topic"</span>, <span class="string">"publish from luat mqtt client"</span>, <span class="number">0</span>)</pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:receive"></a>
    <strong  style="color: red;">
    mqttc:receive (timeout<strong  style="color: green;">[, msg=nil]</strong>)
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">接收消息</pre>


    <h3>Parameters:</h3>
    <ul>
        <li><span class="parameter">timeout</span>
            <span class="types"><span class="type">number</span></span>
        <pre class="example"> 接收超时时间，单位毫秒</pre>
        </li>
        <li><span class="parameter">msg</span>
            <span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
         [<em>此参数可选，默认值为：</em> nil]
        <pre class="example"> 可选参数，控制socket所在的线程退出recv阻塞状态</pre>
        </li>
    </ul>

    <h3>Returns:</h3>
    <ol>
        <li>
        <pre class="example">result 数据接收结果，true表示成功，false表示失败</pre></li>
        <li>
        <pre class="example"> data
                如果result为true，表示服务器发过来的mqtt包
<p>                如果result为false，超时失败,data为"timeout"
                如果result为false，msg控制退出，data为msg的字符串
                如果result为false，socket连接被动断开控制退出，data为"CLOSED"
                如果result为false，PDP断开连接控制退出，data为"IP_ERROR_IND"
<p>                如果result为false，mqtt不处于连接状态，data为nil
                如果result为false，收到了PUBLISH报文，发送PUBACK或者PUBREC报文失败，data为nil
                如果result为false，收到了PUBREC报文，发送PUBREL报文失败，data为nil
                如果result为false，收到了PUBREL报文，发送PUBCOMP报文失败，data为nil
                如果result为false，发送PINGREQ报文失败，data为nil</pre></li>
        <li>
        <pre class="example">param 如果是msg控制退出，param的值是msg的参数；其余情况无意义，为nil</pre></li>
    </ol>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example"><span class="keyword">true</span>, packet = mqttc:receive(<span class="number">2000</span>)
<span class="keyword">false</span>, error_message = mqttc:receive(<span class="number">2000</span>)
<span class="keyword">false</span>, msg, para = mqttc:receive(<span class="number">2000</span>,<span class="string">"APP_SEND_DATA"</span>)</pre></il>
    </ul>

</dd>
    <dt>
    <a name = "mqttc:disconnect"></a>
    <strong  style="color: red;">
    mqttc:disconnect ()
    </strong>
    </dt>
    <dd>
    <pre class="example"  style="color: red;">断开与服务器的连接</pre>



    <h3>Returns:</h3>
    <ul>
        <li>
        <pre class="example">nil</pre></il>
    </ul>



    <h3>Usage:</h3>
    <ul>
        <li><pre class="example">mqttc = mqtt.client(<span class="string">"clientid-123"</span>, <span class="keyword">nil</span>, <span class="keyword">nil</span>, <span class="keyword">false</span>)
mqttc:connect(<span class="string">"mqttserver.com"</span>, <span class="number">1883</span>, <span class="string">"tcp"</span>)
process data
mqttc:disconnect()</pre></il>
    </ul>

</dd>
</dl>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2021-08-02 13:53:19 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>

